//
//  DataBaseManager.m
//  UGoods
//
//  Created by 李贺 on 16/2/25.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "DataBaseManager.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FindModel.h"

@interface DataBaseManager ()

@property (nonatomic, retain) FMDatabase *fmdb;
@property (nonatomic, retain) FindModel *model;

@end

@implementation DataBaseManager

- (void)dealloc
{
    [_fmdb release];
    [_model release];
    [super dealloc];
}

// 单例方法
+ (DataBaseManager *)shareInstance {
    
    // 静态指针变量 保证指针只有一个
    static DataBaseManager *dbManager = nil;
    
    // 是否第一次使用该方法
    // 如果指针为空 创建对象
    if (!dbManager) {
        // 单例中的alloc不需要release
        dbManager = [[DataBaseManager alloc] init];
    }
    // 如果指针不为空 直接返回
    return dbManager;
}

// 打开数据库
- (void)openDB {
    
    // 获取数据库路径
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"fmdb.db"];
    NSLog(@"dbpath: %@", path);
    // 创建数据库对象
    self.fmdb = [[FMDatabase alloc] initWithPath:path];
    // 打开数据库
    BOOL result = [_fmdb open];
    // 判断
    [self judgeResult:result message:@"数据库打开"];
}

// 关闭数据库
- (void)closeDB {
    
    BOOL result = [_fmdb close];
    
    [self judgeResult:result message:@"数据库关闭"];
}

// 创建表
- (void)createTable {
    // SQL
    NSString *createSQL = @"create table if not exists FindModel (goods_id, app_show_logo, goods_name, shop_price)";
    [self executeSQL:createSQL message:@"创建表"];

}

//删除表
- (void)dropTable {
    // SQL
    NSString *dropSQL = @"drop table FindModel";
    [self executeSQL:dropSQL message:@"删除表"];
}

// 增
- (void)insertFindModel:(FindModel *)model {
    // 1.SQL
    NSString *insertSQL = [NSString stringWithFormat:@"insert into FindModel (goods_id, app_show_logo, goods_name, shop_price) values('%@', '%@', '%@', '%@')", model.goods_id, model.app_show_logo, model.goods_name, model.shop_price];
    [self executeSQL:insertSQL message:@"插入"];
}

// 删除
- (void)deleteFindModelWithId:(NSString *)goods_id {
    // 1.SQL
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from FindModel where goods_id = '%@'", goods_id];
    [self executeSQL:deleteSQL message:@"删除"];
}

// 查
- (NSMutableArray *)select {
    // 需要返回的数组
    NSMutableArray *array = [NSMutableArray array];
    // SQL
    NSString *selectSQL = @"select * from FindModel";
    // 执行查询
    FMResultSet *resultSet = [_fmdb executeQuery:selectSQL];
    // 便利信息获取数据
    while ([resultSet next]) {
        NSString *goods_id = [resultSet stringForColumn:@"goods_id"];
        NSString *app_show_logo = [resultSet stringForColumn:@"app_show_logo"];
        NSString *goods_name = [resultSet stringForColumn:@"goods_name"];
        NSString *shop_price = [resultSet stringForColumn:@"shop_price"];
        // 创建对象 保存信息
        FindModel *model = [[FindModel alloc] init];
        model.goods_id = goods_id;
        model.app_show_logo = app_show_logo;
        model.goods_name = goods_name;
        model.shop_price = shop_price;
        // 添加到数组
        [array addObject:model];
    }
    return array;
}

// 判断是否存在
- (BOOL)selectDataWithgoods_id:(NSString *)goods_id {
    NSString *selectSQL = [NSString stringWithFormat:@"select * from FindModel where goods_id = '%@'", goods_id];
    // 执行查询
    FMResultSet *resultSet = [_fmdb executeQuery:selectSQL];
    while ([resultSet next]) {
        NSLog(@"数据库中已存在");
        return YES;
    }
    
    return NO;
}





// 封装执行+判断
/**
 * 参数1：需要执行的SQL语句
 * 参数2：执行之后的打印信息
 */
- (void)executeSQL:(NSString *)sql message:(NSString *)message {
    // 执行
    BOOL result = [_fmdb executeUpdate:sql];
    // 判断
    if (result) {
        NSLog(@"%@成功", message);
    } else {
        NSLog(@"%@失败", message);
    }
}

// 判断方法
/**
 * 参数1：判断数值
 * 参数2：打印信息
 */
- (void)judgeResult:(BOOL)result message:(NSString *)message {
    if (result) {
        NSLog(@"%@成功", message);
    } else {
        NSLog(@"%@失败", message);
    }
}






@end
